﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class Stocks : ClassManager.SessionManagerPage
    {
        private void GetAllStocks()
        {
            StocksGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.StockProvider.GetByCompanyId(getSessionManager().CompanyIDSelectedComboBox);
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.StockColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.StocksGrid.MasterTableView.SortExpressions.AddSortExpression(expression);               
            }
        }

        protected void StocksGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllStocks();

            StocksGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
        }

        protected void StocksGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {                
                GPSTrackingDBV2.Entities.Stock StockEntity = ClassManager.InvokeEntityManager.Invoke_Stock_Entity(e);
                StockEntity.CompanyId = getSessionManager().CompanyIDSelectedComboBox;
                StockEntity.StockId = GPSTrackingDBV2Custom.Data.DataRepository.StockProvider.GetNextID(getSessionManager().CompanyIDSelectedComboBox);
                GPSTrackingDBV2.Data.DataRepository.StockProvider.Insert(StockEntity);
            }
            catch (Exception ex)
            {                
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede insertar la información.");
            }
        }

        protected void StocksGrid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Stock StockEntity = ClassManager.InvokeEntityManager.Invoke_Stock_Entity(e);

                StockEntity.OriginalCompanyId = StockEntity.CompanyId;
                StockEntity.OriginalStockId = StockEntity.StockId;

                GPSTrackingDBV2.Data.DataRepository.StockProvider.Update(StockEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede modificar la información.");
            }
        }

        protected void StocksGrid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GPSTrackingDBV2.Entities.Stock StockEntity = ClassManager.InvokeEntityManager.Invoke_Stock_Entity(e);
                GPSTrackingDBV2.Data.DataRepository.StockProvider.Delete(StockEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
                showErrorMessage("No se puede eliminar la información.");
            }
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            StocksGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in StocksGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }


            StocksGrid.MasterTableView.Rebind();
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Dispositivos ...');", true);
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }
    }
}